﻿using System;
using System.Globalization;
using System.Text;
using System.Web.UI;

using Sunyi.Project.WebPoint.BusinessLogic;
using Sunyi.Project.WebPoint.Common;
using Sunyi.Project.WebPoint.DataContract;
using Sunyi.Project.WebPoint.WebControl;

using ActivitySystem.BusinessLogic;
using ActivitySystem.Common;
using ActivitySystem.DataContract;

namespace ActivitySystem.Web.Activity
{
    public partial class View : WebPointPage
    {
        private ActivityManager activityManager = new ActivityManager();
        private UserManager userManager = new UserManager();
        private ParticipationManager participationManager = new ParticipationManager();
        private ActivityTypeManager activityTypeManager = new ActivityTypeManager();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                try
                {
                    if (!string.IsNullOrEmpty(Request.Params[ActivitySystemGlobalConstant.ActivityId]))
                    {
                        Guid activityId = new Guid(Request.Params[ActivitySystemGlobalConstant.ActivityId]);
                        BindActivity(activityId);
                    }
                }
                catch (Exception ex)
                {
                    ExceptionHandler.Handle(this, ex);
                }
            }
        }

        #region Private Method

        private void BindActivity(Guid activityId)
        {
            var activity = activityManager.Select(activityId);
            var launcher = userManager.Select(activity.LauncherId);

            bool participated = false;
            StringBuilder sbParticipants = new StringBuilder();
            foreach (Guid participationId in activity.Participations)
            {
                var participation = participationManager.Select(participationId);
                var user = userManager.Select(participation.UserId);

                if (user == CurrentUser)
                {
                    tbxFellow.Text = participation.Fellow.ToString();
                    participated = true;
                }

                sbParticipants.Append(user.UserName);
                sbParticipants.Append(WebPointGlobalConstant.LeftBracket);
                sbParticipants.Append(ActivitySystemGlobalConstant.Plus);
                sbParticipants.Append(participation.Fellow + 1);
                sbParticipants.Append(WebPointGlobalConstant.RightBracket);
                sbParticipants.Append(WebPointGlobalConstant.Comma);
                sbParticipants.Append(WebPointGlobalConstant.Space);
            }
            string participants = sbParticipants.ToString();
            participants = participants.TrimEnd(WebPointGlobalConstant.Space.ToCharArray());
            participants = participants.TrimEnd(WebPointGlobalConstant.Comma.ToCharArray());

            StringBuilder sbActivityTypes = new StringBuilder();
            foreach (Guid activityTypeId in activity.ActivityTypes)
            {
                var activityType = activityTypeManager.Select(activityTypeId);
                sbActivityTypes.Append(activityType.Name);
                sbActivityTypes.Append(WebPointGlobalConstant.Comma);
                sbActivityTypes.Append(WebPointGlobalConstant.Space);
            }
            string activityTypes = sbActivityTypes.ToString();
            activityTypes = activityTypes.TrimEnd(WebPointGlobalConstant.Space.ToCharArray());
            activityTypes = activityTypes.TrimEnd(WebPointGlobalConstant.Comma.ToCharArray());

            if (activity != null)
            {
                tbxId.Text = activity.Id.ToString();
                tbxTitle.Text = activity.Title;
                tbxActivityType.Text = activityTypes;
                tbxParticipants.Text = participants;
                tbxDescription.Text = activity.Description;
                tbxLauncher.Text = launcher.UserName;
                tbxStartTime.Text = activity.StartTime.ToString(ActivitySystemGlobalConstant.TimeEditFormatString, CultureInfo.InvariantCulture);
                tbxEndTime.Text = activity.EndTime.ToString(ActivitySystemGlobalConstant.TimeEditFormatString, CultureInfo.InvariantCulture);
            }

            if (participated)
            {
                btnJoin.Text = "Modify & Rejoin!";
            }
            else
            {
                if (!activity.IsPublic)
                {
                    btnJoin.Text = "Invite only!";
                    btnJoin.Enabled = false;
                }
                btnQuit.Visible = false;
            }

            if (activity.LauncherId != CurrentUser.Id)
            {
                btnDismiss.Visible = false;
            }
            else
            {
                btnQuit.Visible = false;
            }

            if (!activity.IsActive || activity.EndTime < DateTime.Now)
            {
                ltlClosed.Visible = true;

                tbxFellow.Enabled = false;
                tbxComments.Enabled = false;

                btnDismiss.Enabled = false;
                btnJoin.Enabled = false;
                btnQuit.Enabled = false;
            }
        }

        #endregion

        protected void btnJoin_Click(object sender, EventArgs e)
        {
            try
            {
                int fellow = 0;
                if (!string.IsNullOrEmpty(tbxFellow.Text) && !int.TryParse(tbxFellow.Text, out fellow))
                {
                    ShowClientMessage("Fellow is not correct. Please enter a non-negative number.");
                }

                string comments = tbxComments.Text;

                if (!string.IsNullOrEmpty(Request.Params[ActivitySystemGlobalConstant.ActivityId]))
                {
                    Guid activityId = new Guid(Request.Params[ActivitySystemGlobalConstant.ActivityId]);
                    participationManager.JoinActivity(CurrentUser.Id, activityId, fellow, comments);
                }
                ClientRedirect(Request.UrlReferrer.AbsoluteUri);
            }
            catch (Exception ex)
            {
                ExceptionHandler.Handle(this, ex);
            }
        }

        protected void btnQuit_Click(object sender, EventArgs e)
        {
            try
            {
                if (!string.IsNullOrEmpty(Request.Params[ActivitySystemGlobalConstant.ActivityId]))
                {
                    Guid activityId = new Guid(Request.Params[ActivitySystemGlobalConstant.ActivityId]);
                    participationManager.QuitActivity(CurrentUser.Id, activityId);
                }
                ClientRedirect(Request.UrlReferrer.AbsoluteUri);
            }
            catch (Exception ex)
            {
                ExceptionHandler.Handle(this, ex);
            }
        }

        protected void btnDismiss_Click(object sender, EventArgs e)
        {
            try
            {
                if (!string.IsNullOrEmpty(Request.Params[ActivitySystemGlobalConstant.ActivityId]))
                {
                    Guid activityId = new Guid(Request.Params[ActivitySystemGlobalConstant.ActivityId]);
                    activityManager.Dismiss(CurrentUser.Id, activityId);
                }
                ClientRedirect(Request.UrlReferrer.AbsoluteUri);
            }
            catch (Exception ex)
            {
                ExceptionHandler.Handle(this, ex);
            }
        }
    }
}